home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / algen100.zip / ALGEN.C next >
C/C++ Source or Header  |  1993-03-01  |  15KB  |  332 lines

  1. /* ALGEN.C -- Public Domain -- By Richard Butler -- EUROPA Software 1992
  2.  
  3.     This code is public domain. Do with it what you please.
  4.     All of the code is ANSI C and should compile with little or no change
  5.     on any ANSI C compiler.
  6.  
  7.     This code carries no warranties, whether expressed or implied.
  8.  
  9.     Thanks to Harvey Parisien for his HLIST program upon which ALGEN is based.
  10.     ALGEN's .cfg format and file list output are nearly identical to HLIST.
  11.  
  12.     Thanks to Erik Vanriper for releasing the source for his FILELIST program.
  13.     It was a very useful reference for writing ALGEN.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include <sys\types.h>
  19. #include <sys\stat.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23.  
  24. char *whitespace = " \t\n\r";
  25.  
  26. main(int argc, char *argv[])
  27. {
  28.     char system_name[40];
  29.     char header_file[80];
  30.     int new_days;
  31.     char list_file[80];
  32.     char new_list_file[80];
  33.     struct xx {
  34.         char path[80];
  35.         char desc[80];
  36.         char files_bbs[80];
  37.     };
  38.     struct xx *area[256];
  39.     struct stat info;
  40.     struct tm current_date;
  41.     struct tm new_date;
  42.     struct tm *tm_ptr;
  43.     time_t current_time;
  44.     time_t new_time;
  45.     unsigned area_count = 0;
  46.     unsigned new_area_count = 0;
  47.     unsigned total_count = 0;
  48.     unsigned total_new_count = 0;
  49.     unsigned long area_bytes = 0;
  50.     unsigned long new_area_bytes = 0;
  51.     unsigned long total_bytes = 0;
  52.     unsigned long total_new_bytes = 0;
  53.     FILE *config_file_p;
  54.     FILE *header_file_p;
  55.     FILE *list_file_p;
  56.     FILE *new_list_file_p;
  57.     FILE *fp;
  58.     char config_file[80] = "ALGEN.CFG";     /* Default config file */
  59.     int i = 0;
  60.     size_t len;
  61.     char io_buf[256];
  62.     char *ptr;
  63.     char *ws_ptr;
  64.     int num_areas;
  65.     char file_path[128];
  66.     char file_name[13];
  67.     unsigned char new;
  68.     char *fd_ptr;
  69.     int length;
  70.     char stripflags;
  71.  
  72.     printf("\n╓─────────────────────────────────────────────────────────────────────────────╖");
  73.     printf("\n║ ALGEN v1.00 - by Richard Butler - EUROPA Software          Fidonet 1:396/61 ║");
  74.     printf("\n╙─────────────────────────────────────────────────────────────────────────────╜");
  75.     fflush(stdout);
  76.     for(i=0; i < 256; i++) {
  77.         if((area[i] = malloc(sizeof(struct xx))) == NULL) {
  78.             printf("\n\nNot enough memory!  Exiting....");
  79.             exit(255);
  80.         }
  81.     }
  82.     current_time = time(NULL);
  83.     tm_ptr = localtime(¤t_time);
  84.     current_date = *tm_ptr;
  85.     if(argc > 1)            /* assume that argv[1] is a valid config file */
  86.         strcpy(config_file, argv[1]);
  87.     if((config_file_p = fopen(config_file,"r")) == NULL) {
  88.         printf("\n\nError opening %s!  Exiting....",config_file);
  89.         exit(255);
  90.     }
  91.  
  92.     /* Parse config file */
  93.  
  94.     printf("\n\nReading %s....",config_file);
  95.     fflush(stdout);
  96.     do {                                        /* get system name */
  97.         fgets(io_buf,256,config_file_p);
  98.     } while(io_buf[0] == ';');
  99.     strncpy(system_name,io_buf,39);
  100.     if(ptr = strchr(system_name,'\n'))
  101.         *ptr = '\0';
  102.     do {                                        /* get header file path */
  103.         fgets(io_buf,256,config_file_p);
  104.     } while(io_buf[0] == ';');
  105.     strncpy(header_file,io_buf,79);
  106.     if(ptr = strchr(header_file,'\n'))
  107.         *ptr = '\0';
  108.     do {                                        /* get new days */
  109.         fgets(io_buf,256,config_file_p);
  110.     } while(io_buf[0] == ';');
  111.     new_days = atoi(io_buf);
  112.     new_time = (current_time - (new_days * 86400));
  113.     tm_ptr = localtime(&new_time);
  114.     new_date = *tm_ptr;
  115.     do {                                        /* get file list path */
  116.         fgets(io_buf,256,config_file_p);
  117.     } while(io_buf[0] == ';');
  118.     strncpy(list_file,io_buf,79);
  119.     if(ptr = strchr(list_file,'\n'))
  120.         *ptr = '\0';
  121.     do {                                        /* get new file list path */
  122.         fgets(io_buf,256,config_file_p);
  123.     } while(io_buf[0] == ';');
  124.     strncpy(new_list_file,io_buf,79);
  125.     if(ptr = strchr(new_list_file,'\n'))
  126.         *ptr = '\0';
  127.     do {                                        /* get STRIPFLAGS keyword */
  128.         fgets(io_buf,256,config_file_p);
  129.     } while(io_buf[0] == ';');
  130.     if(ptr = strchr(io_buf,'\n'))
  131.         *ptr = '\0';
  132.     for(i = 0; io_buf[i] != '\0'; i++)
  133.         io_buf[i] = toupper(io_buf[i]);
  134.     stripflags = !(strcmp("STRIPFLAGS",io_buf));
  135.  
  136.     /* get files.bbs paths and area descriptions */
  137.  
  138.     i = 0;
  139.     while(fgets(io_buf,256,config_file_p) != NULL) {
  140.         if(io_buf[0] == ';')
  141.             continue;
  142.         strncpy(area[i]->path,io_buf,79);
  143.         len = strcspn(area[i]->path,whitespace);
  144.         area[i]->path[len] = '\0';
  145.         if(area[i]->path[len - 1] != '\\')
  146.             strncat(area[i]->path,"\\",79 - len);
  147.         if(ptr = strchr(io_buf, ' ')) {
  148.             ptr += (strspn(ptr,whitespace));
  149.             strcpy(area[i]->desc," ");
  150.             strncat(area[i]->desc,ptr,77);
  151.         }
  152.         if(ptr = strchr(area[i]->desc, '^')) {
  153.             *ptr++ = ' ';
  154.             *ptr = '\0';
  155.             if(ptr = strchr(io_buf, '^'))
  156.                 strncpy(area[i]->files_bbs,++ptr,79);
  157.             if(ptr = strchr(area[i]->files_bbs, '\n'))
  158.                 *ptr = '\0';
  159.         }
  160.         else if(ptr = strchr(area[i]->desc, '\n')) {
  161.             *ptr++ = ' ';
  162.             *ptr = '\0';
  163.             strcpy(area[i]->files_bbs,area[i]->path);
  164.             strncat(area[i]->files_bbs,"FILES.BBS",78 - len);
  165.         }
  166.         num_areas = ++i;
  167.     }
  168.     fclose(config_file_p);
  169.     printf("\n\nProcessing....\n");
  170.     fflush(stdout);
  171.  
  172.     /* open output files */
  173.  
  174.     if((list_file_p = fopen(list_file,"w")) == NULL) {
  175.         printf("\n\nError opening %s!  Exiting....",list_file);
  176.         exit(255);
  177.     }
  178.     if((new_list_file_p = fopen(new_list_file,"w")) == NULL) {
  179.         printf("\n\nError opening %s!  Exiting....",new_list_file);
  180.         exit(255);
  181.     }
  182.     fprintf(list_file_p,"\n                              F I L E    L I S T\n");
  183.     fprintf(list_file_p,"\n %-53s%s",system_name,ctime(¤t_time));
  184.     fprintf(list_file_p,"╓─────────────────────────────────────────────────────────────────────────────╖\n");
  185.     fprintf(list_file_p,"║        This is a complete list of all files available on this system        ║\n");
  186.     fprintf(list_file_p,"╙─────────────────────────────────────────────────────────────────────────────╜\n");
  187.     if((header_file_p = fopen(header_file,"r")) != NULL) {
  188.         while(fgets(io_buf,256,header_file_p) != NULL)
  189.             fputs(io_buf,list_file_p);
  190.         fclose(header_file_p);
  191.     }
  192.     else {
  193.         printf("\nError opening %s!  Continuing....",header_file);
  194.         fflush(stdout);
  195.     }
  196.     fprintf(list_file_p,"╓─────────────────────────────────────────────────────────────────────────────╖\n");
  197.     sprintf(io_buf,"║   File dates followed by a * are < than %d days old, which is >= %.2d-%.2d-%d",new_days,(new_date.tm_mon + 1),new_date.tm_mday,new_date.tm_year);
  198.     strcat(io_buf,"          ");
  199.     io_buf[78] = '║';
  200.     io_buf[79] = '\0';
  201.     fprintf(list_file_p,"%s\n",io_buf);
  202.     fprintf(list_file_p,"║                                                                             ║\n");
  203.     fprintf(list_file_p,"║ ALGEN v1.00 - by Richard Butler - EUROPA Software          Fidonet 1:396/61 ║\n");
  204.     fprintf(list_file_p,"╙─────────────────────────────────────────────────────────────────────────────╜\n");
  205.     fprintf(new_list_file_p,"\n                              F I L E    L I S T\n");
  206.     fprintf(new_list_file_p,"\n %-53s%s",system_name,ctime(¤t_time));
  207.     fprintf(new_list_file_p,"╓─────────────────────────────────────────────────────────────────────────────╖\n");
  208.     fprintf(new_list_file_p,"║         This is a list of all 'NEW' files available on this system          ║\n");
  209.     fprintf(new_list_file_p,"║                                                                             ║\n");
  210.     sprintf(io_buf,"║             Files are < than %d days old, which is >= %.2d-%.2d-%d",new_days,(new_date.tm_mon + 1),new_date.tm_mday,new_date.tm_year);
  211.     strcat(io_buf,"               ");
  212.     io_buf[78] = '║';
  213.     io_buf[79] = '\0';
  214.     fprintf(new_list_file_p,"%s\n",io_buf);
  215.     fprintf(new_list_file_p,"║                                                                             ║\n");
  216.     fprintf(new_list_file_p,"║ ALGEN v1.00 - by Richard Butler - EUROPA Software          Fidonet 1:396/61 ║\n");
  217.     fprintf(new_list_file_p,"╙─────────────────────────────────────────────────────────────────────────────╜\n");
  218.  
  219.     /* process files.bbs */
  220.  
  221.     for(i = 0 ; i < num_areas ; i++) {
  222.         area_count = 0;
  223.         new_area_count = 0;
  224.         area_bytes = 0;
  225.         new_area_bytes = 0;
  226.         strcpy(io_buf,"▒▒▒▒▒▒▒▒▒▒▒▒");
  227.         strcat(io_buf,area[i]->desc);
  228.         strcat(io_buf,"▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒");
  229.         fprintf(list_file_p,"\n%.79s\n",io_buf);
  230.         fprintf(new_list_file_p,"\n%.79s\n",io_buf);
  231.         if((fp = fopen(area[i]->files_bbs,"r")) == NULL) {
  232.             printf("\nError opening %s!  Continuing....",area[i]->files_bbs);
  233.             fflush(stdout);
  234.         }
  235.         else {
  236.             printf("\n\t%s",area[i]->desc);
  237.             fflush(stdout);
  238.             while(fgets(io_buf,256,fp) != NULL) {
  239.                 if(io_buf[0] == '-' || io_buf[0] == ' ' || io_buf[0] == '\n') {
  240.                     if(ptr = strchr(io_buf, '\n'))
  241.                         *ptr = '\0';
  242.                     io_buf[79] = '\0';
  243.                     fprintf(list_file_p,"\n%s",io_buf);
  244.                 }
  245.                 else {
  246.                     strncpy(file_name,io_buf,12);
  247.                     if(ptr = strpbrk(file_name,whitespace))
  248.                         *ptr = '\0';
  249.                     strcpy(file_path,area[i]->path);
  250.                     strcat(file_path,file_name);
  251.                     if(stat(file_path,&info) == -1)
  252.                         fprintf(list_file_p,"\n%-12s (stored offline) ",file_name);
  253.                     else {
  254.                         tm_ptr = localtime(&info.st_mtime);
  255.                         fprintf(list_file_p,"\n%-12s %7ld %.2d-%.2d-%d",file_name,info.st_size,(tm_ptr->tm_mon + 1),tm_ptr->tm_mday,tm_ptr->tm_year);
  256.                         area_count++;
  257.                         total_count++;
  258.                         area_bytes += info.st_size;
  259.                         total_bytes += info.st_size;
  260.                         if(new = ((current_time / 86400) - (info.st_mtime / 86400)) <= new_days) {
  261.                             fprintf(new_list_file_p,"\n%-12s %7ld %.2d-%.2d-%d",file_name,info.st_size,(tm_ptr->tm_mon + 1),tm_ptr->tm_mday,tm_ptr->tm_year);
  262.                             fprintf(list_file_p,"*");
  263.                             fprintf(new_list_file_p,"*");
  264.                             new_area_count++;
  265.                             total_new_count++;
  266.                             new_area_bytes += info.st_size;
  267.                             total_new_bytes += info.st_size;
  268.                         }
  269.                         else
  270.                             fprintf(list_file_p," ");
  271.                     }
  272.                     if(fd_ptr = strchr(io_buf, ' ')) {
  273.                         fd_ptr += (strspn(fd_ptr,whitespace));
  274.                         if(stripflags && (fd_ptr[0] == '/'))
  275.                             fd_ptr = strpbrk(fd_ptr,whitespace);
  276.                         if(ptr = strtok(fd_ptr,whitespace)) {
  277.                             length = 30;
  278.                             do {
  279.                                 if((length + strlen(ptr) + 1) < 80) {
  280.                                     fprintf(list_file_p," %s",ptr);
  281.                                     if(new)
  282.                                         fprintf(new_list_file_p," %s",ptr);
  283.                                     length += (strlen(ptr) + 1);
  284.                                 }
  285.                                 else {
  286.                                     fprintf(list_file_p,"\n                              ");
  287.                                     fprintf(list_file_p," %s",ptr);
  288.                                     if(new) {
  289.                                         fprintf(new_list_file_p,"\n                              ");
  290.                                         fprintf(new_list_file_p," %s",ptr);
  291.                                     }
  292.                                     length = 31 + strlen(ptr);
  293.                                 }
  294.                                 ptr = strtok('\0',whitespace);
  295.                             } while(ptr);
  296.                         }
  297.                     }
  298.                 }
  299.             }
  300.             fclose(fp);
  301.         }
  302.         fprintf(list_file_p,"\n\n  ** %d files in this area (%ld bytes)\n\n",area_count,area_bytes);
  303.         fprintf(new_list_file_p,"\n\n  ** %d new files in this area (%ld bytes)\n\n",new_area_count,new_area_bytes);
  304.     }
  305.  
  306.     /* finish things up */
  307.  
  308.     fprintf(list_file_p,"\n %-53s%s",system_name,ctime(¤t_time));
  309.     fprintf(list_file_p,"╓─────────────────────────────────────────────────────────────────────────────╖\n");
  310.     sprintf(io_buf,"║ There are a total of %d files on this system (%ld bytes)",total_count,total_bytes);
  311.     strcat(io_buf,"                               ");
  312.     io_buf[78] = '║';
  313.     io_buf[79] = '\0';
  314.     fprintf(list_file_p,"%s\n",io_buf);
  315.     fprintf(list_file_p,"║                                                                             ║\n");
  316.     fprintf(list_file_p,"║ ALGEN v1.00 - by Richard Butler - EUROPA Software          Fidonet 1:396/61 ║\n");
  317.     fprintf(list_file_p,"╙─────────────────────────────────────────────────────────────────────────────╜\n");
  318.     fprintf(new_list_file_p,"\n %-53s%s",system_name,ctime(¤t_time));
  319.     fprintf(new_list_file_p,"╓─────────────────────────────────────────────────────────────────────────────╖\n");
  320.     sprintf(io_buf,"║ There are a total of %d new files on this system (%ld bytes)",total_new_count,total_new_bytes);
  321.     strcat(io_buf,"                               ");
  322.     io_buf[78] = '║';
  323.     io_buf[79] = '\0';
  324.     fprintf(new_list_file_p,"%s\n",io_buf);
  325.     fprintf(new_list_file_p,"║                                                                             ║\n");
  326.     fprintf(new_list_file_p,"║ ALGEN v1.00 - by Richard Butler - EUROPA Software          Fidonet 1:396/61 ║\n");
  327.     fprintf(new_list_file_p,"╙─────────────────────────────────────────────────────────────────────────────╜\n");
  328.     fclose(list_file_p);
  329.     fclose(new_list_file_p);
  330.     printf("\n\n....Done!\n");
  331. }
  332.